<template>
	<view class="logins">
		<nav-bar isBack backColor="#fff" title="手机登录" color="#fff" background="transparent" isPlace="false"></nav-bar>
		
		<!-- 顶部 -->
		<view class="phonetop">
			<view class="l_top">
				<image src="https://manager.ryz1620.com/static/ryzLogo/logo-2.png"></image>
			</view>
			<view class="conmt">
				<view class="call">
					<text class="left-text">手机号</text>
					<input type="number" placeholder="请输入手机号" v-model="mobile"/>
				</view>
				<view class="call">
					<text class="left-text">验证码</text>
					<input type="number" placeholder="请输入验证码" v-model="sms_code"/>
					<text class="right-text"  v-if="time===60"@click="getCodeEvent">获取验证码</text>
					<text class="right-text"  v-if="time!==60" style="color: #999;">{{time}}s</text>
				</view>
				<view class="loginbtn" @click="userBindMobile">登录</view>
			</view>
		</view>
	</view>
</template>

<script>
import navBar from "@/components/navBar/navBar.vue"
import service_enter from '@/service/login.js'
import URL from '@/config/url.js'


export default {
	components: { navBar },
	
	data() {
		return {
			mobile:'',
			sms_code:'',
			time:60,
			timer:null,
			// 登录信息
			token: null,           // token
			isBindMobile: null,    // 是否绑定手机号
			userMobile: null,      // 用户手机号
			userId: null,          // 用户userId
			goods:'',
			ww_share_code:''
		}
	},
	
	onLoad(op) {
		if(op.goods){
			this.goods = op.goods
			this.ww_share_code = op.ww_share_code
		}
	},
	
	methods: {
		getCodeEvent(){
				let phonenum = /^1[23456789]\d{9}$/.test(this.mobile);
				if(!this.mobile){
					uni.showToast({ title: '请输入手机号', icon: 'none' });
					return
				}
				if(!phonenum){
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
					return
				}
				
				service_enter.getPhonecode({
					mobile:this.mobile
				}).then(res=>{
					if(res.code === 1){
						this.timeInterval();
						uni.showToast({ title: '已发送验证码！', icon: 'none' });
					}else{
						uni.showToast({ title: res.msg, icon: 'none' });
					}
					
				})
			},
			timeInterval(){
				this.time = this.time - 1;
				this.timer = setInterval(()=>{
					if(this.time-1<1){
						clearInterval(this.timer)
						this.timer = null;
						this.time = 60
					}else{
						this.time = this.time - 1;
					}
				},1000)
			},
			userBindMobile(){
				let single_id = uni.getStorageSync('single_id')
				let root = uni.getStorageSync('root')=="true"?true:false
				let product_id = uni.getStorageSync('id')
				let gid = uni.getStorageSync('gid')
				let u = uni.getStorageSync('u')
				let NewName = uni.getStorageSync('NewName')
				let phonenum = /^1[23456789]\d{9}$/.test(this.mobile);
				let shareuId = uni.getStorageSync('shareuId')
				if(!this.mobile){
					uni.showToast({ title: '请输入手机号', icon: 'none' });
					return
				}
				if(!phonenum){
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
					return
				}
				if(!this.sms_code){
					uni.showToast({ title: '请输入验证码', icon: 'none' });
					return
				}
				this.$utils.showLoading()
				service_enter.getUserPhone({
					mobile:this.mobile,
					sms_code:this.sms_code,
					share_id:shareuId
				}).then(res=>{
					this.$utils.hideLoading()
					if (res.code == 1) {
						let data = res.data
						let mobile = res.data.mobile
						// 保存数据
						let token = data.token.access_token
						let userId = data.user_id
						let userMobile = data.mobile
						this.openid = ''
						// 保存数据
						this.token = token
						this.userMobile = userMobile
						this.userId = userId
						let nowOpenid = res.data.openid
						uni.setStorageSync('token', token);
						uni.setStorageSync('userMobile', userMobile);
						uni.setStorageSync('userId', userId);
						uni.setStorageSync('token', token);
						uni.setStorageSync('nowOpenid', nowOpenid);
						that.$store.commit('index/SET_nowOpenid',nowOpenid)
						this.$store.commit('index/SET_token', token)
						this.$store.commit('index/SET_userMobile', userMobile)
						this.$store.commit('index/SET_userId', userId)
						this.isBindMobile = true
						this.userMobile = mobile
						this.$store.commit('index/SET_isBindMobile', true)
						this.$store.commit('index/SET_userMobile', mobile)
						let Router = uni.getStorageSync('Router')
						let urls = '/' + Router
						console.log(urls)
						if(urls=='/pages/home/index/index'){
							uni.switchTab({
									url: '/pages/home/index/index'
							 })
						}else if (urls=='/pages/discovery/index/index'){
							uni.switchTab({
									url: '/pages/discovery/index/index'
							 })
						}else if (urls=='/pages/mall/index/index'){
							uni.switchTab({
									url: '/pages/mall/index/index'
							 })
						}else if (urls=='/pages/my/index'){
							uni.switchTab({
									url: '/pages/my/index'
							 })
						}
						else if (urls=='/pages/home/index/list'){
							uni.switchTab({
									url: '/pages/home/index/list'
							 })
						}else{
							uni.redirectTo({
								url: urls
							});
						}
						// console.log(single_id,product_id)
						// 跳转页面
						// console.log(NewName);
						// if (single_id) {
						// 	if (root) {
						// 		uni.redirectTo({
						// 			url: `/pages_mall/pages/mall/userPay/userPay?single_id=${single_id}&root=true`
						// 		})
						// 	} else {
						// 		uni.redirectTo({
						// 			url: '/pages_mall/pages/mall/userPay/userPay?single_id=' +
						// 				single_id
						// 		})
						// 	}
						// } else if (this.goods) {
						// 	uni.redirectTo({
						// 		url: `/pages_mall/pages/mall/goods_details/goods_details?id=${product_id}&gid=${gid}&u=${u}&ww_share_code=${this.ww_share_code}`
						// 	})
						// } else if (product_id && NewName) {
						// 	uni.redirectTo({
						// 		url: `/pages_mall/pages/mall/goods_details/GoodsShareDetails?id=${product_id}&gid=${gid}&u=${u}`
						// 	})
						// } else {
						// 	uni.switchTab({
						// 		url: '/pages/home/index/index'
						// 	});
						// }
					} else {
						this.$utils.hideLoading()
						this.$utils.showErr({
							title: res.msg
						})
					}
				})
			}
		},
		onUnload() {
			clearInterval(this.timer)
			this.timer = null;
		}

	
}
</script>

<style lang="scss" scoped>
.logins{
	width: 100vw;height: 100vh;background: #f8f8f8;display: flex;flex-flow: column;
}
.phonetop{
	padding: 0 40rpx;
	padding-top:220rpx;
	background: url(https://cos.ryz1620.com/liankun/static/beijing.png) no-repeat top center;
	background-size: 100%;
}
.l_top{
	image{
		width: 250rpx;
		height: 250rpx;
		display: block;
		margin: auto;
		background: #fff;
		padding: 20rpx;
		border-radius: 50%;
	}
		background: url(https://cos.ryz1620.com/static/login/bgt@2x.png) no-repeat bottom center;
		background-size: 100%;
}
.conmt{
		background: #fff;
		padding: 30rpx;
		padding-bottom: 80rpx;
		border-radius: 0 0 20rpx 20rpx;
		.call{
			position: relative;
			.left-text{
				display: block;
				font-size: 30rpx;
				font-weight: bold;
				line-height: 70rpx;
			}
			input{
				background: #F8F8F8;
				height: 80rpx;
				line-height: 80rpx;
				padding: 0 20rpx;
				margin-bottom: 50rpx;
			}
			.right-text{
				display:inline-block;
				text-align: center;
				position: absolute;
				right: 20rpx;
				bottom: 0;
				line-height: 80rpx;
				color: #DBA954;
				z-index: 99;
			}
		}
		.loginbtn{
			line-height: 88rpx;
			border-radius: 44rpx;
			text-align: center;
			background: #3D444B;
			font-size: 32rpx;
			color: #fff;
		}
}
</style>